package com.beiding.jsonanalyzer.analyze.query.queryfunc;

import com.beiding.jsonanalyzer.analyze.query.Comparator;
import com.beiding.jsonanalyzer.analyze.query.ComparatorResult;

import java.util.Date;

/**
 * @author 丁常磊
 * @date 2022/6/27 10:57
 */

public class Le implements Comparator {

    @Override
    public ComparatorResult compare(Object keyValue, Object valueValue) {
        if (keyValue instanceof Number && valueValue instanceof Number) {
            return ((Number) keyValue).doubleValue() <= ((Number) valueValue).doubleValue() ? ComparatorResult.TRUE : ComparatorResult.FALSE;
        } else if (keyValue instanceof String && valueValue instanceof String) {
            return ((String) keyValue).compareTo((String) valueValue) <= 0 ? ComparatorResult.TRUE : ComparatorResult.FALSE;
        } else if (keyValue instanceof Date && valueValue instanceof Date) {
            return ((Date) keyValue).getTime() <= ((Date) valueValue).getTime() ? ComparatorResult.TRUE : ComparatorResult.FALSE;
        }
        return null;
    }

    @Override
    public String toString() {
        return "<=";
    }
}
